//
//  ST_LiveBeautyHeader.swift
//  btvideo
//
//  Created by mac on 2021/5/7.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_LiveBeautyHeader: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(19)
            make.left.right.equalToSuperview()
            make.height.equalTo(80)
//                .equalToSuperview().inset(UIEdgeInsets(top: 19, left: 0, bottom: 0, right: 0))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        let controlLayout = UICollectionViewFlowLayout()
        controlLayout.scrollDirection = .horizontal
        controlLayout.minimumLineSpacing = 9
//        controlLayout.minimumInteritemSpacing = 9
        
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 13, bottom: 0, right: 13)
        
        controlLayout.itemSize = CGSize.init(width: 120, height: 80)
        
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_LiveBeautyHeaderCell.self)
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = UIColor.white
//        control.alwaysBounceVertical = true
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    fileprivate var lastItem:ST_LiveBeautyHeaderModel?
    
    var data_array = [ST_LiveBeautyHeaderModel](){
        didSet{
            
            print("多次BUG  ---")
            
            lastItem = data_array.first
            self.collectionView.reloadData()
        }
    }
    
    
    var pickIdClousre:((Int)->())?
    
    
}


//MARK: -
extension ST_LiveBeautyHeader:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_LiveBeautyHeaderCell.self, for: indexPath)
        
        cell.model = data_array[safe:indexPath.row]
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let model = data_array[safe: indexPath.row]
        
        lastItem?.isSelected = false
        lastItem = model
        lastItem?.isSelected = true
        
        collectionView.reloadData()
        
        guard let id = model?.id else { return  }
        
        self.pickIdClousre?(id)
        
    }
    
}

//MARK: - Cell
class ST_LiveBeautyHeaderCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        contentView.backgroundColor = .clear
        contentView.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 15
        control.borderColor = UIColor(hex: 0x4A4A4A)
        control.borderWidth = 2
          return control
      }()
    
    weak var model:ST_LiveBeautyHeaderModel?{
        didSet{
            
            
            guard let m = model else { return  }
            
            self.cover_image_view.sd_setImage(with: m.bigImage.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            if m.isSelected{
                self.cover_image_view.borderColor = UIColor(hex: 0x4A4A4A)
            }else{
                self.cover_image_view.borderColor = .clear
            }
        }
    }
}


//MARK: - Model


class ST_LiveBeautyHeaderModel {

    
    var desc : String
    var id : Int
    var bigImage : String
    var midImage : String
    var image : String
    var name : String
    var weight : Int
    var isSelected = false

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        desc = json["desc"].stringValue
        id = json["id"].intValue
        name = json["name"].stringValue
        weight = json["weight"].intValue
        
        image = json["image"].stringValue
        bigImage = json["big_image"].stringValue
        midImage = json["middle_image"].stringValue
    
    }
    
    class func create_recommend_data()->ST_LiveBeautyHeaderModel{
        
        let model = ST_LiveBeautyHeaderModel(fromJson: JSON.null)
        model.id = 0
        model.bigImage = "http://btstatic.jingniaoapps.com/rz5nsx21620478042"
        return model
    }
    
    
}
